import React, { Component } from "react";
import Header from "../../components/Header/Header";
import { reqlogin } from "../../http/api";
import { Button, Toast } from "antd-mobile";
export default class Login extends Component {
  constructor() {
    super();
    this.state = {
      user: {
        phone: "",
        password: "",
      },
    };
  }
  changeUser(e,key){
    this.setState({
      user:{
        ...this.state.user,
        [key]:e.target.value
      }
    })
  }
 async login(){
      let res=await reqlogin(this.state.user);
      if(res.data.code==200){
        Toast.show({
          content:res.data.msg
        })
        localStorage.setItem("isLogin", JSON.stringify(res.data.list));
        this.props.history.push("/index/home")
      }
  }

  render() {
    let { user } = this.state;

    return (
      <div>
        {" "}
        <Header register title="登录" {...this.props}></Header>
        <div>user: {JSON.stringify(user)}</div>
        <div>
          账号： <input type="text" onChange={(e)=>{
                this.changeUser(e,'phone')
          }} value={user.phone}   placeholder="账号"/>
        </div>
        <div>
          密码： <input type="text"onChange={(e)=>{
            this.changeUser(e,"password")
          }}  value={user.password} placeholder="请输入密码"/>
        </div>
        <Button onClick={()=>this.login()}>登录</Button>
      </div>
    );
  }
}
